<?php

namespace api\controllers;

use common\models\User;
use common\models\Installing;
use common\models\Exchange;

class AccountController extends ApiController {

    /**
     * 
     */
    public function beforeAction($action) {
        parent::beforeAction($action);
        $this->checkLogin();
        return true;
    }

    /**
     * get user
     */
    public function actionUser() {
        $id = \Yii::$app->request->get('uid');
        $user = User::find()->where(['id' => $id])->one();
        if ($user) {
            $result = [
                "message" => '',
                "status" => "ok",
                "total" => 1,
                "objects" => [
                        0 => [
                            "id" => $user->id,
                            "email" => $user->email,
                            "country" => $user->country,
                            "date_share" => $user->date_share,
                            "device_id" => $user->device_id,
                            "username" => $user->username,
                            "manufacturer" => $user->manufacturer,
                            "model" => $user->model,
                            "phonetype" => $user->phonetype,
                            "token" => $user->token,
                            "totalPayouts" =>  $user->totalPayouts,
                            "total_coins" => $user->total_coins,
                            "created" => date('Y-m-d',strtotime($user->created)),
                            "status" => $user->status
                        ]
                    ],
            ];
        } else {
            $result = [
                "message" => "User don't exists!",
                "status" => "error",
                "total" => 0,
                "objects" => null,
            ];
        }
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }

    /**
     * change username
     */
    public function actionChangeusername() {
        $id =  \Yii::$app->request->post('uid');
        $status = "error";
        $messages = ["User don't exists!"];
        $user = User::find()->where(['id' => $id])->one();
        if ($user) {
            $user->username = \Yii::$app->request->post('username');
            if ($user->save()) {
                $status = "ok";
                $messages = ["success"];
            }else{
                $messages = $user->getErrors();
            }
        }
        $result = [
            "message" => $this->parserMessage($messages),
            "status" => $status,
            "total" => 0,
            "objects" => null,
        ];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /***
     * 
     */
    public function actionEarningHistory(){
        $status = "ok";
        $messages = [];
        $objects = [];
        $total_page = 0;
        $limit = 20;
        $id = \Yii::$app->request->get('uid');
        $page = (int) \Yii::$app->request->get('page', 0);
        //check user
        $user = User::find()->where(['id' => $id])->one();
        if(!$user){
            $status = "error";
            $messages[] = "The user don't exists!";
        }else{
            $total_record = Installing::find()->where(['user_id' => $id])->count();
            if ($total_record) {
                $total_page = ((int) ($total_record / $limit)) + (($total_record % $limit) > 0 ? 1 : 0);
                $installs = Installing::find()->where(['user_id' => $id])->offset(($page * $limit))->limit($limit)->all();
                foreach($installs as $install){
                    $objects[] = [
                        'created' => date('Y-m-d',strtotime($install->created)),
                        'id' => $install->id,
                        'img' => \Yii::$app->params['BaseUrlImg'].$install->campaign->id.".png",
                        'name' => $install->campaign->name,
                        'point' => $install->point,
                        'rating' => floatval(str_replace(',', '.', $install->campaign->rating)),
                    ];
                }
                $messages[] = "success";
            }
        }
        
        $result = [
            "message" => $this->parserMessage($messages),
            "total_page" => $total_page,
            "status" => $status,
            "total" => count($objects),
            "objects" => count($objects) > 0?$objects:null,
        ];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
    }
    
    /**
     * 
     */
    public function actionPayoutHistory(){
        $status = "ok";
        $messages = "success";
        $objects = [];
        $id = \Yii::$app->request->get('uid');
        //check user
        $user = User::find()->where(['id' => $id])->one();
        if(!$user){
            $status = "error";
            $messages = "The user don't exists!";
        }
        
        $exchanges = Exchange::find()->where(['user_id' => $id])->all();
        foreach($exchanges as $exchange){
            $objects[] = [
                'created' => date('Y-m-d',  strtotime($exchange->created)),
                'gift_name' => $exchange->gift_name,
                'id' => $exchange->id,
                'point' => $exchange->point,
                'status' => $exchange->status,
                'type' => $exchange->type,
                'code' => $exchange->code,
                'serial_code' => $exchange->serial_code
            ];
        }
        
        $result = [
            "message" => $this->parserMessage($messages),
            "status" => $status,
            "total" => count($objects),
            "objects" => count($objects) > 0?$objects:null,
        ];
        $this->setHeader(200);
        echo json_encode($result);
        exit();
        
    }

}
